﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public delegate void PriceClickEventHandler(object sender, EventArgs e);

    public partial class CustomerPrice : System.Web.UI.UserControl
    {
        private int intCustomerID = -1;
        private int intCommand = -1;
        private bool isAudit = false;
        private bool isUpdate = false;

        public bool IsAudit
        {
            get { return isAudit; }
            set { isAudit = value; }
        }
        public int CustomerID
        {
            get { return intCustomerID; }
            set { intCustomerID = value; }
        }
        public int Command
        {
            get { return intCommand; }
            set { intCommand = value; }
        }
        public string M_ID
        {
            get { return this.txtID.Text; }
            set { this.txtID.Text = value; }
        }
        public string M_Name
        {
            get { return this.txtName.Text; }
            set { this.txtName.Text = value; }
        }
        public int PriceType
        {
            get { return Int32.Parse(this.dropPrice.SelectedItem.Value); }
            set { this.dropPrice.SelectedIndex = value; }
        }
        public string Remark
        {
            get { return this.txtRemark.Text; }
            set { this.txtRemark.Text = value; }
        }
        public bool AddPanelVisible
        {
            get { return this.panAddResult.Visible; }
            set { this.panAddResult.Visible = value; }
        }
        public bool IsUpdate
        {
            get { return this.isUpdate; }
            set { this.isUpdate = value; }
        }

        public event PriceClickEventHandler AddClick;
        public event PriceClickEventHandler CalcenClick;
        public event PriceClickEventHandler UpdateClick;

        protected void OnClick(EventArgs e)
        {
            if (AddClick != null) AddClick(this, e);
            if (CalcenClick != null) CalcenClick(this, e);
            if (UpdateClick != null) UpdateClick(this, e);
        }

        public void ChangPage(object sender, EventArgs e)
        {
            this.OnClick(e);

            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            DataSet dst = cls_Property.SetOrGetChildResultDataSet;

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                string strKey = ((LinkButton)sender).CommandName;
                int intPageIndex = this.dgrdResult.CurrentPageIndex;
                int intPageCounts = this.dgrdResult.PageCount;

                if (strKey == "0")
                {
                    intPageIndex = 0;
                }
                else if (strKey == "1")
                {
                    if (intPageIndex > 0) intPageIndex -= 1;
                }
                else if (strKey == "2")
                {
                    if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
                }
                else
                {
                    intPageIndex = intPageCounts - 1;
                }

                this.BindCustomersPrice(dst, intPageIndex);
            }
        }

        public void UpdateInfo(object sender, ImageClickEventArgs e)
        {
            this.OnClick(e);
            //this.GetCustomersPriceInfo(this.CustomerID);

            ImageButton ibtn = (ImageButton)sender;
            int intCommand = Int32.Parse(ibtn.CommandName);
            int intIndex = Int32.Parse(ibtn.CommandArgument);
            int intID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());

            CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
            ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
            ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
            ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");
            CuTextSelectBox txtRsID = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("txtRsID");
            CuTextBox txtRsName = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("txtRsName");
            CuTextBox txtRsRemark = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("txtRsRemark");
            CuTextBox dropRsTxt = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("dropRsTxt");
            DropDownList dropRsPrice = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("dropRsPrice");

            if (intCommand == 0)//打开编辑
            {
                chkRsSelect.Visible = ibtnRsEdit.Visible = dropRsTxt.Visible = false;
                ibtnRsCancel.Visible = ibtnRsServer.Visible = dropRsPrice.Visible = true;
                txtRsRemark.ReadOnly = false;
                this.lblJs.Text = "<script>document.getElementById('" + dropRsPrice.ClientID + "').focus();</script>";
            }
            else if (intCommand == 2)//保存
            {
                ClsCustomersInfoDAL cls_Customer = new ClsCustomersInfoDAL();
                Boolean is_Succeed = cls_Customer.AddOrUpdateCustomerPrice(intID, this.CustomerID, txtRsID.Text.Trim(), Int32.Parse(dropRsPrice.SelectedItem.Value), txtRsRemark.Text.Trim());

                if (is_Succeed)
                {
                    this.isUpdate = false;
                }
                else
                {
                    this.lblJs.Text = "<script>alert('修改客户商品价格等级失败，请重试！');</script>";
                }
            }
            else
            {
                this.isUpdate = false;
            }
        }

        private void DeleteInfo(int Cus_ID, int Type)
        {
            ClsCustomersInfoDAL cls_Customer = new ClsCustomersInfoDAL();
            int intCount = this.dgrdResult.Items.Count - 1;

            for (int i = intCount; i >= 0; i--)
            {
                bool isChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

                if (isChecked)
                {
                    int intID = Int32.Parse(this.dgrdResult.Items[i].Cells[1].Text.Trim());
                    Boolean is_Succeed = cls_Customer.DeleteCustomerPrice(intID, Type);
                }
            }
        }

        private void GetCustomersPriceInfo(int Cus_ID)
        {
            ClsCustomersInfoDAL cls_Customers = new ClsCustomersInfoDAL();
            DataSet dst = cls_Customers.GetCustomersPrice(Cus_ID);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                ClsPropertyDAL cls_Property = new ClsPropertyDAL();
                cls_Property.SetOrGetChildResultDataSet = dst;

                this.BindCustomersPrice(dst, 0);
            }
            else
            {
                this.dgrdResult.CurrentPageIndex = 0;
                this.dgrdResult.DataSource = null;
                this.dgrdResult.DataBind();
            }
        }

        private void BindCustomersPrice(DataSet dst, int CurrPageIndex)
        {
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                this.dgrdResult.CurrentPageIndex = CurrPageIndex;
                this.dgrdResult.DataSource = dst;
                this.dgrdResult.DataBind();

                this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
                this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
                this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = true;
                this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = true;

                if (this.dgrdResult.PageCount == 1)
                {
                    this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
                }
                else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
                {
                    this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
                }
                else if (this.dgrdResult.CurrentPageIndex == 0)
                {
                    this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblJs.Text = "";//清空Js脚本
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();

            string strUserID = cls_Property.SetOrGetUserID;

            if (strUserID != "")
            {
                if (this.intCommand == 0)//新增
                {
                    this.panAddResult.Visible = true;
                    this.lblJs.Text = "<script>document.getElementById('" + this.txtID.ClientID + "_txtBox').focus();</script>";
                }
                else if (this.intCommand == 1)//作废
                {
                    this.DeleteInfo(this.intCustomerID, 0);
                }
                else if (this.intCommand == 2)//彻底作废
                {
                    this.DeleteInfo(this.intCustomerID, 1);
                }

                if (!this.isUpdate)
                {
                    this.GetCustomersPriceInfo(this.intCustomerID);//获取客户价格信息
                }
            }
            else
            {
                this.Page.Session.Remove("SetOrGetUserID");
                this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
            }
        }

        protected void ibtnAddCancel_Click(object sender, ImageClickEventArgs e)
        {
            this.OnClick(e);
        }

        protected void ibtnAddServer_Click(object sender, ImageClickEventArgs e)
        {
            this.OnClick(e);
            this.GetCustomersPriceInfo(this.intCustomerID);//获取客户价格信息
        }

        protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                CheckBox chkRsSelect = (CheckBox)e.Item.FindControl("chkRsSelect");
                ImageButton ibtnRsEdit = (ImageButton)e.Item.FindControl("ibtnRsEdit");
                CuTextBox dropRsTxt = (CuTextBox)e.Item.FindControl("dropRsTxt");
                DropDownList dropRsPrice = (DropDownList)e.Item.FindControl("dropRsPrice");

                //从表可操作性控制
                if (IsAudit)
                {
                    chkRsSelect.Enabled = ibtnRsEdit.Enabled = false;
                    ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
                }

                int intPriceType = Int32.Parse(e.Item.Cells[2].Text.Trim());
                dropRsPrice.SelectedIndex = intPriceType;
                dropRsTxt.Text = dropRsPrice.SelectedItem.Text;
            }
        }
    }
